package cn.kgc.springcloud.service.impl;

import cn.kgc.springcloud.client.TagClient;
import cn.kgc.springcloud.entity.Clazz;
import cn.kgc.springcloud.entity.Tag;
import cn.kgc.springcloud.mapper.ClazzMapper;
import cn.kgc.springcloud.service.ClazzService;
import cn.kgc.springcloud.vo.ClazzVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/8/30
 */
@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Autowired
    private TagClient tagClient;

    @Override
    public Clazz addClazz(Clazz clazz) {
         clazzMapper.addClazz(clazz);
        return clazz;
    }

    @Override
    public List<ClazzVO> findAll() {

        List<Clazz> clazzs =  clazzMapper.findAll();
        ArrayList<ClazzVO> clazzVOS = new ArrayList<>();

        clazzs.forEach(clazz->{
            ClazzVO clazzVO = new ClazzVO();
            BeanUtils.copyProperties(clazz, clazzVO);
            //获取clazz的tagId  根据tagI的查询tag的具体信息
            Integer tagId = clazz.getTagId();
            // 调用tag服务 根据id查询tag的信息
            Tag tag = tagClient.findTagById(tagId);
            clazzVO.setTag(tag);
            //clazzVo 添加到集合
            clazzVOS.add(clazzVO);
        });
        return clazzVOS;
    }

    @Override
    public Boolean deleteClazz(Integer clazzId) {
       Integer line=  clazzMapper.deleteById(clazzId);
        return line >0;
    }

    @Override
    public Clazz selectById(Integer clazzId) {
        Clazz clazz =  clazzMapper.selectById(clazzId);
        return clazz;
    }
}
